/**   
 * @Title: xUtils.java 
 * @Package com.police.http 
 * @Description: TODO(用一句话描述该文件做什么) 
 * @author liming  
 * @date 2016年6月16日 下午2:26:36 
 * @version V1.0   
 */
package com.police.http;

import java.util.Map;

import org.xutils.x;
import org.xutils.common.Callback.Cancelable;
import org.xutils.common.Callback.CommonCallback;
import org.xutils.http.RequestParams;

/**
 * @ClassName: xUtils
 * @Description: 对xUtils网络请求进行封装
 * @author liming
 * @date 2016年6月16日 下午2:26:36
 * 
 */
public class xUtils
{
    /**
     * 发送get请求
     * 
     * @param <T>
     */
    public static <T> Cancelable Get(String url, Map<String, String> map,
            CommonCallback<T> callback)
    {
        RequestParams params = new RequestParams(url);
        if (null != map)
        {
            for (Map.Entry<String, String> entry : map.entrySet())
            {
                params.addQueryStringParameter(entry.getKey(), entry.getValue());
            }
        }
        Cancelable cancelable = x.http().get(params, callback);
        return cancelable;
    }

    /**
     * 发送post请求
     * 
     * @param <T>
     */
    public static <T> Cancelable Post(String url, Map<String, Object> map,
            CommonCallback<T> callback)
    {
        RequestParams params = new RequestParams(url);
        if (null != map)
        {
            for (Map.Entry<String, Object> entry : map.entrySet())
            {
                params.addParameter(entry.getKey(), entry.getValue());
            }
        }
        Cancelable cancelable = x.http().post(params, callback);
        return cancelable;
    }

    /**
     * 上传文件
     * 
     * @param <T>
     */
    public static <T> Cancelable UpLoadFile(String url,
            Map<String, Object> map, CommonCallback<T> callback)
    {
        RequestParams params = new RequestParams(url);
        if (null != map)
        {
            for (Map.Entry<String, Object> entry : map.entrySet())
            {
                params.addParameter(entry.getKey(), entry.getValue());
            }
        }
        params.setMultipart(true);
        Cancelable cancelable = x.http().get(params, callback);
        return cancelable;
    }

    /**
     * 下载文件
     * 
     * @param <T>
     */
    public static <T> Cancelable DownLoadFile(String url, String filepath,
            CommonCallback<T> callback)
    {
        RequestParams params = new RequestParams(url);
       
        // 设置断点续传
        params.setAutoResume(true);
        params.setSaveFilePath(filepath);
        Cancelable cancelable = x.http().get(params, callback);
        return cancelable;
    }
}


